@echo off
setlocal enabledelayedexpansion

echo ========================================
echo        Cursor编辑器缓存清理工具
echo        适用于Cursor 0.46+版本
echo ========================================
echo.

:: 设置颜色
color 0A

:: 检查管理员权限
net session >nul 2>&1
if %errorLevel% == 0 (
    echo [信息] 已获取管理员权限，继续执行...
) else (
    echo [警告] 未检测到管理员权限，某些操作可能失败
    echo 建议右键以管理员身份运行此脚本
    echo.
    pause
)

echo [信息] 开始清理Cursor缓存文件...
echo.

:: 关闭所有Cursor进程
echo [步骤1] 检查并关闭所有Cursor进程...
tasklist /fi "imagename eq cursor.exe" | find /i "cursor.exe" > nul
if %errorlevel% equ 0 (
    echo 发现正在运行的Cursor进程，正在关闭...
    taskkill /f /im cursor.exe > nul 2>&1
    echo Cursor进程已关闭
) else (
    echo 未发现正在运行的Cursor进程
)

:: 清理主要缓存目录
echo.
echo [步骤2] 清理Cursor缓存目录...

:: 设置缓存路径
set "APPDATA_CACHE=%APPDATA%\Cursor"
set "LOCALAPPDATA_CACHE=%LOCALAPPDATA%\Cursor"
set "USERPROFILE_CACHE=%USERPROFILE%\.cursor"

:: 清理AppData\Roaming下的缓存
if exist "%APPDATA_CACHE%" (
    echo 清理 %APPDATA_CACHE%\Cache ...
    if exist "%APPDATA_CACHE%\Cache" rd /s /q "%APPDATA_CACHE%\Cache" 2>nul
    
    echo 清理 %APPDATA_CACHE%\Code Cache ...
    if exist "%APPDATA_CACHE%\Code Cache" rd /s /q "%APPDATA_CACHE%\Code Cache" 2>nul
    
    echo 清理 %APPDATA_CACHE%\GPUCache ...
    if exist "%APPDATA_CACHE%\GPUCache" rd /s /q "%APPDATA_CACHE%\GPUCache" 2>nul
    
    echo 清理 %APPDATA_CACHE%\CachedData ...
    if exist "%APPDATA_CACHE%\CachedData" rd /s /q "%APPDATA_CACHE%\CachedData" 2>nul
    
    echo 清理 %APPDATA_CACHE%\logs ...
    if exist "%APPDATA_CACHE%\logs" rd /s /q "%APPDATA_CACHE%\logs" 2>nul
) else (
    echo 未找到 %APPDATA_CACHE% 目录
)

:: 清理AppData\Local下的缓存
if exist "%LOCALAPPDATA_CACHE%" (
    echo 清理 %LOCALAPPDATA_CACHE%\Cache ...
    if exist "%LOCALAPPDATA_CACHE%\Cache" rd /s /q "%LOCALAPPDATA_CACHE%\Cache" 2>nul
    
    echo 清理 %LOCALAPPDATA_CACHE%\Code Cache ...
    if exist "%LOCALAPPDATA_CACHE%\Code Cache" rd /s /q "%LOCALAPPDATA_CACHE%\Code Cache" 2>nul
    
    echo 清理 %LOCALAPPDATA_CACHE%\GPUCache ...
    if exist "%LOCALAPPDATA_CACHE%\GPUCache" rd /s /q "%LOCALAPPDATA_CACHE%\GPUCache" 2>nul
    
    echo 清理 %LOCALAPPDATA_CACHE%\Crashpad ...
    if exist "%LOCALAPPDATA_CACHE%\Crashpad" rd /s /q "%LOCALAPPDATA_CACHE%\Crashpad" 2>nul
) else (
    echo 未找到 %LOCALAPPDATA_CACHE% 目录
)

:: 清理用户目录下的.cursor文件夹
if exist "%USERPROFILE_CACHE%" (
    echo 清理 %USERPROFILE_CACHE%\cache ...
    if exist "%USERPROFILE_CACHE%\cache" rd /s /q "%USERPROFILE_CACHE%\cache" 2>nul
    
    echo 清理 %USERPROFILE_CACHE%\logs ...
    if exist "%USERPROFILE_CACHE%\logs" rd /s /q "%USERPROFILE_CACHE%\logs" 2>nul
    
    echo 清理 %USERPROFILE_CACHE%\GPUCache ...
    if exist "%USERPROFILE_CACHE%\GPUCache" rd /s /q "%USERPROFILE_CACHE%\GPUCache" 2>nul
    
    echo 清理 %USERPROFILE_CACHE%\CachedExtensions ...
    if exist "%USERPROFILE_CACHE%\CachedExtensions" rd /s /q "%USERPROFILE_CACHE%\CachedExtensions" 2>nul
    
    echo 清理 %USERPROFILE_CACHE%\CachedExtensionVSIXs ...
    if exist "%USERPROFILE_CACHE%\CachedExtensionVSIXs" rd /s /q "%USERPROFILE_CACHE%\CachedExtensionVSIXs" 2>nul
) else (
    echo 未找到 %USERPROFILE_CACHE% 目录
)

:: 清理临时文件
echo.
echo [步骤3] 清理Cursor相关临时文件...
del /f /s /q "%TEMP%\Cursor-*" 2>nul

:: 清理完成
echo.
echo [完成] Cursor缓存清理完毕!
echo.
echo 建议定期运行此脚本以保持Cursor的流畅运行
echo 如果清理后仍然卡顿，请考虑重新安装Cursor或升级系统配置
echo.
echo ========================================
echo        感谢使用Cursor缓存清理工具
echo ========================================

pause
exit 